import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {ActivatedRoute, Router} from "@angular/router";
import {MaintainService} from "../../shared/service/maintain.service";
import {MaintenanceService} from "../../shared/service/maintenance.service";
import {MaintainRecord} from "../../shared/model/maintainRecord";
import {Vehicle} from "../../shared/model/vehicle";

@Component({
  selector: 'app-maintain-record',
  templateUrl: './maintain-record.component.html',
  styleUrls: ['./maintain-record.component.css']
})
export class MaintainRecordComponent implements OnInit {

  maintainForm:FormGroup;
  oid:string = this.routerInfo.snapshot.queryParams["oid"];
  userId:string = this.routerInfo.snapshot.queryParams["userId"];
  maintainRecord:MaintainRecord;
  veh:Vehicle;

  constructor(
    private router:Router,
    private routerInfo: ActivatedRoute,
    private fb:FormBuilder,
    private maintainService:MaintainService,
  ) { }

  ngOnInit() {
    this.createForm();
    this.maintainService.getMaintainRecordByOid(this.oid).then(res=>{
      console.log(res);
      this.maintainRecord = res.maintainRecord;
      this.veh = res.vehicle;
      let maintainTypeName="";
      if(this.maintainRecord.maintainTypeObj){
        maintainTypeName = this.maintainRecord.maintainTypeObj.name;
      }
      this.maintainForm.reset({
        oid: this.maintainRecord.oid,
        vehicle:this.veh.oid,
        vehicleName:this.veh.name,
        licenseNumber:this.veh.licenseNumber,
        starttime:this.maintainRecord.starttime,
        endtime:this.maintainRecord.endtime,
        maintainType:maintainTypeName,
        maintainCost:this.maintainRecord.maintainCost,
        maintainItem:this.maintainRecord.maintainItem,
        description:this.maintainRecord.description
      });
    });
  }

  createForm(){
    this.maintainForm =this.fb.group({
      oid:'',
      vehicle:'',
      vehicleName:'',
      licenseNumber:'',
      starttime:'',
      endtime:'',
      maintainType:'',
      maintainCost:'',
      maintainItem:'',
      description:''
    });
  }

  comeBack(){
    this.router.navigate(["camCalendar/camCalendar"],{queryParams:{userId:this.userId}});
  }
}
